package requests

import (
	"WeChatMGRA/modul"
	"encoding/json"
	"errors"
	"gitee.com/yutiandou/wdtools/wdlog"
	"github.com/valyala/fasthttp"
)

//SendText ..发送文本消息和图片
func SendText(wid,wcid,context string,isImage bool)error{
	req := fasthttp.AcquireRequest()
	defer fasthttp.ReleaseRequest(req) // 用完需要释放资源
	req.Header.SetContentType("application/json")
	req.Header.SetMethod("POST")
	req.Header.Add("Authorization",authorization)
	test_data := modul.GetText_Request(wid,wcid,context)
	req.SetBody(test_data)
	if isImage {
		req.SetRequestURI(urlhead+"sendImage")
	}else {
		req.SetRequestURI(urlhead+"sendText")
	}
	resp := fasthttp.AcquireResponse()
	defer fasthttp.ReleaseResponse(resp)
	if err := fasthttp.Do(req, resp); err != nil {
		return err
	}
	resp_data := resp.Body()
	testmsg := modul.GetBaseMsg()
	if err := json.Unmarshal(resp_data,testmsg);err!=nil{
		return err
	}
	if testmsg.Code == "1000"{
		return nil
	}
	modul.GiveBaseMsg(testmsg)
	return errors.New("发送错误")
}
//DownImage 下载图片
func DownImage(wid,content,fromuser,touser string,msgid int64)string{
	data,err := MakeARequest("getMsgImg",modul.GetImage_Down(wid,content,fromuser,touser,msgid))
	if wdlog.Error(err){
		return "null"
	}
	img := modul.GetBaseMsg()
	if err := json.Unmarshal(data,img);	wdlog.Error(err){
		return "null"
	}
	if img.Code == "1000"{
		return img.Data.(string)
	}
	modul.GiveBaseMsg(img)
	return "null"
}
//SendText ..发送视频
func SendImage(wid,wcid,content string)error{
	resp_data,err := MakeARequest("sendRecvImage",modul.GetMV_Request(wid,wcid,content))
	if err!=nil{
		return err
	}
	mvmsg := modul.GetBaseMsg()
	if err := json.Unmarshal(resp_data,mvmsg);err!=nil{
		return err
	}
	if mvmsg.Code == "1000"{
		return nil
	}
	modul.GiveBaseMsg(mvmsg)
	return errors.New("发送错误")
}

//SendText ..发送视频
func SendMV(wid,wcid,content string)error{
	resp_data,err := MakeARequest("sendRecvVideo",modul.GetMV_Request(wid,wcid,content))
	if err!=nil{
		return err
	}
	mvmsg := modul.GetBaseMsg()
	if err := json.Unmarshal(resp_data,mvmsg);err!=nil{
		return err
	}
	if mvmsg.Code == "1000"{
		return nil
	}
	modul.GiveBaseMsg(mvmsg)
	return errors.New("发送错误")
}
//DownMV 下载视频
func DownMV(wid,content string,msgid int64)string{
	data,err := MakeARequest("getMsgVideo",modul.GetMV_Down(wid,content,msgid))
	if wdlog.Error(err){
		return "null"
	}
	img := modul.GetBaseMsg()
	if err := json.Unmarshal(data,img);	wdlog.Error(err){
		return "null"
	}
	if img.Code == "1000"{
		return img.Data.(string)
	}
	modul.GiveBaseMsg(img)
	return "null"
}
//SendText ..发送语音
func SendVoice(wid,wcid,content string,len int)error{
	resp_data,err := MakeARequest("sendVoice",modul.GetVoice_Request(wid,wcid,content,len))
	//wdlog.Debugln(string(modul.GetVoice_Request(wid,wcid,content,len)))
	if err!=nil{
		return err
	}
	vociemsg := modul.GetBaseMsg()
	if err := json.Unmarshal(resp_data,vociemsg);err!=nil{
		return err
	}
	if vociemsg.Code == "1000"{
		return nil
	}
	modul.GiveBaseMsg(vociemsg)
	return errors.New("发送错误")
}

//DownVoice 下载语音
func DownVoice(wid string,msgid,length int64)string{
	data,err := MakeARequest("getMsgVoice",modul.GetVoice_Down(wid,msgid,length))
	if wdlog.Error(err){
		return "null"
	}
	img := modul.GetBaseMsg()
	if err := json.Unmarshal(data,img);	wdlog.Error(err){
		return "null"
	}
	if img.Code == "1000"{
		return img.Data.(string)
	}
	modul.GiveBaseMsg(img)
	return "null"
}